//
//  GKHomeTableViewController.swift
//  MicroBlogFunc
//
//  Created by GK on 16/4/8.
//  Copyright © 2016年 GK. All rights reserved.
//

import UIKit
import SVProgressHUD


class GKHomeTableViewController: GKBaseTableViewController {

    var arrStatus:[GKStatus]?
    
    //刷新控件view
    private var pullDownRefreshView : GKPullDownRefreshView = GKPullDownRefreshView()
    
    let reusedID = "HomeTableViewCell"
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        //假如没有登录 不要设置下面的
      if  !GKUserAccountViewModel.shareInstance.userLogin{
            return  }
        
        setUpNavItem()
        
        //添加刷新控件view
        tableView.addSubview(pullDownRefreshView)
        //去掉分割线
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        
        //注册cell 
        tableView.registerClass(GKStatusCell.self, forCellReuseIdentifier: reusedID)
        //设置自动适应行高
        tableView.rowHeight = UITableViewAutomaticDimension
        //预估行高
        tableView.estimatedRowHeight = 200
        
        
        //请求微博数据
        GKStatusViewModel.shareInstance.loadStatus { (status, error) in
            
            if error != nil
            {
                GKPrint("获取微博数据失败了\(error)")
                SVProgressHUD.showErrorWithStatus("您的网络不给力")
                return
            }
            self.arrStatus = status
//            GKPrint("获取的微博数据\(status)")
            self.tableView.reloadData()
        }
    }
    
    //添加导航按钮
    private func setUpNavItem()
    {
        navigationItem.leftBarButtonItem = UIBarButtonItem(imgName: "navigationbar_friendsearch", selectedImgName: "navigationbar_friendsearch_highlighted", target: self, action: #selector(self.didClickPerson))
        navigationItem.rightBarButtonItem = UIBarButtonItem(imgName: "navigationbar_pop", selectedImgName: "navigationbar_pop_highlighted", target: self, action: #selector(self.didClickQRCode))
       
        let name = GKUserAccountViewModel.shareInstance.userAccount?.screen_name ?? "没有用户名"
        
        let btn = GKHomeTitleBtn(title: name)
        btn.addTarget(self, action: #selector(self.didClickBtnTitle(_:)), forControlEvents: UIControlEvents.TouchDown)
        navigationItem.titleView = btn
    }
    
    //按钮点击
    @objc private func didClickBtnTitle(btn:UIButton)
    {
        btn.selected = !btn.selected
        UIView.animateWithDuration(GKAnimateDuration) {
            
            btn.imageView?.transform =  btn.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.00001)) : CGAffineTransformIdentity
        }
    }
    
    @objc private func didClickPerson()
    {
//        GKPrint("点击了人头")
    }
    @objc private func didClickQRCode()
    {
//        GKPrint("点击了人头右边")
    }
    
}

extension GKHomeTableViewController
{
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.arrStatus?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        //转成自定义cell
        let cell = tableView.dequeueReusableCellWithIdentifier(reusedID, forIndexPath: indexPath) as! GKStatusCell
        cell.status = self.arrStatus?[indexPath.row]
        return cell
    }
    
    //不要高亮点击效果
    override func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return false
    }
    
}










